<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\GoodsType;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Input;
use App\Http\Requests\Admin\goodsType\StoreRequest;
use App\Http\Requests\Admin\goodsType\UpdateRequest;
use Gate;
use App\Exceptions\Admin\PermissionDeniedException;

class GoodsTypeController extends Controller
{
    public function goods_type_gate()
    {
        if (!Gate::allows('goods','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $this->goods_type_gate();

        $goodsType = GoodsType::paginate(10);
        return view('admin.goods_type.index',compact('goodsType'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(GoodsType $goodsType)
    {
        $this->goods_type_gate();

        return view('admin.goods_type.create',compact('goodsType'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreRequest $request,GoodsType $goodsType)
    {
        $this->goods_type_gate();

        $param = $request->all();
        $goodsType->create($param);
        return Redirect('admin/goods_type');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($type_id)
    {
      
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($type_id)
    {

        $this->goods_type_gate();

        $goodsType = GoodsType::find($type_id);

        return view('admin.goods_type.edit',compact('goodsType'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateRequest $request, $type_id)
    {
        $this->goods_type_gate();

        $goodsType = GoodsType::find($type_id);        

        $goodsType->update($request->all());

        return Redirect('admin/goods_type');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($type_id)
    {

        $this->goods_type_gate();

        $goodsType = GoodsType::find($type_id);

        $goodsType->delete();

        return Redirect('admin/goods_type');
    }

    public function toggleEnabled(){

        $this->goods_type_gate();

        $type_id = Input::get("type_id");
        $enabled = GoodsType::where("type_id",$type_id)->pluck("enabled");        
        $data = array();

        if($enabled==0){
            $data['enabled'] = 1;
            $data['type_id'] = $type_id;
            $res = GoodsType::where("type_id",$type_id)->update(['enabled'=>1]);
        }else{
            $data['enabled'] = 0;
            $data['type_id'] = $type_id;
            $res = GoodsType::where("type_id",$type_id)->update(['enabled'=>0]);
        }
        if($res != false){
            $data['info'] = true;           
        }else{
            $data['info'] = false;
        }
        return $data;

    }
}
